package com.zhanggm.springcloud.jpa.controller;

import com.zhanggm.springcloud.jpa.dao.ClassesDao;
import com.zhanggm.springcloud.jpa.dao.HobbyDao;
import com.zhanggm.springcloud.jpa.dao.StudentDao;
import com.zhanggm.springcloud.jpa.dao.UserRepository;
import com.zhanggm.springcloud.jpa.entity.Classes;
import com.zhanggm.springcloud.jpa.entity.Hobby;
import com.zhanggm.springcloud.jpa.entity.Student;
import com.zhanggm.springcloud.jpa.entity.UserEntity;
import com.zhanggm.springcloud.jpa.service.StudentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.PipedReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Api(tags = "学生管理接口")
@RestController
@RequestMapping("/student/")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "学生分页查询接口",notes = "默认第1页，分页条数2")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页",required = true),
            @ApiImplicitParam(name = "size",value = "分页大小",required = false)
    })
    @ApiResponses({
            @ApiResponse(code = 400,message = "数据类型转换错误，请检查参数类型和格式"),
            @ApiResponse(code = 500,message = "后台发生错误，请检查请求的参数是否正确"),
            @ApiResponse(code=404,message = "接口不存在，请检查接口地址是否正确")
    })
    @GetMapping("getPageInfo")
    public Page<Student> getPageInfo(Student student,
                                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "size",defaultValue = "2") Integer size){
        String str = new String("abc");
        return studentService.getPageInfo(student,page,size);
    }


    @Autowired
    private ClassesDao classesDao;


    @GetMapping("classesList")
    public List<Classes> classesList(){
        return classesDao.findAll();
    }


    @Autowired
    private HobbyDao hobbyDao;

    @GetMapping("hobbyList")
    public List<Hobby> hobbyList(){
        return hobbyDao.findAll();
    }


    @Autowired
    private StudentDao studentDao;

//    @RequestMapping(value = "add",method = RequestMethod.GET)
    @PostMapping("add")
    public Student add(@RequestBody Student student){
        System.out.println(student);
        Student stu = studentDao.save(student);
        return stu;
    }

    @PostMapping("batchDel")
    public boolean batchDel(@RequestBody Student student){
        List<Student> allById = studentDao.findAllById(student.getIds());
        studentDao.deleteAll(allById);
        return true;
    }

}
